package org.jeecg.modules.chvegetable.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.chvegetable.entity.FlowerTagBook;
import org.jeecg.modules.chvegetable.service.IFlowerTagBookService;
import org.jeecg.modules.flower.entity.FlowerTagInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @Description: 采后台账表
* @Author: jeecg-boot
* @Date:   2023-02-07
* @Version: V1.0
*/
@Slf4j
@Api(tags="采后台账表")
@RestController
@RequestMapping("/flower/flowerTagBook")
public class FlowerTagBookController extends JeecgController<FlowerTagBook, IFlowerTagBookService> {
   @Autowired
   private IFlowerTagBookService flowerTagBookService;

   /**
    * 分页列表查询
    *
    * @param flowerTagBook
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "采后台账表-分页列表查询")
   @ApiOperation(value="采后台账表-分页列表查询", notes="采后台账表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(FlowerTagBook flowerTagBook,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  @RequestParam(value = "sDate",defaultValue = "") String sDate ,
                                  @RequestParam(value = "eDate",defaultValue = "") String eDate,
                                  HttpServletRequest req) {
//       QueryWrapper<FlowerTagBook> queryWrapper = QueryGenerator.initQueryWrapper(flowerTagBook, req.getParameterMap());
       Page<FlowerTagBook> page = new Page<FlowerTagBook>(pageNo, pageSize);
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

       LambdaQueryWrapper<FlowerTagBook> queryWrapper = new LambdaQueryWrapper<>();
       if(StringUtils.isNotBlank( flowerTagBook.getFlowerPoleLength() )){
           queryWrapper.eq(FlowerTagBook::getFlowerPoleLength,flowerTagBook.getFlowerPoleLength() );
       }
       if(StringUtils.isNotBlank( flowerTagBook.getSourceBase() )){
           queryWrapper.eq(FlowerTagBook::getSourceBase,flowerTagBook.getSourceBase() );
       }
       if(StringUtils.isNotBlank( flowerTagBook.getProductionId() )){
           queryWrapper.eq(FlowerTagBook::getProductionId,flowerTagBook.getProductionId() );
       }
       if(StringUtils.isNotBlank( flowerTagBook.getVariety() )){
           queryWrapper.eq(FlowerTagBook::getVariety,flowerTagBook.getVariety() );
       }
       if(StringUtils.isNotBlank( flowerTagBook.getVarietyCode() )){
           queryWrapper.eq(FlowerTagBook::getVarietyCode,flowerTagBook.getVarietyCode() );
       }
       if(StringUtils.isNotBlank( flowerTagBook.getCost() )){
           queryWrapper.eq(FlowerTagBook::getCost,flowerTagBook.getCost() );
       }
       if(StringUtils.isNotBlank( flowerTagBook.getStorehouse() )){
           queryWrapper.eq(FlowerTagBook::getStorehouse,flowerTagBook.getStorehouse() );
       }

       if (StringUtils.isNotBlank(sDate)) {
           queryWrapper.apply(" date_format( sort_time, '%y-%m-%d') >=  STR_TO_DATE('"+sDate+"','%Y-%c-%d')  " );
       }
       if (StringUtils.isNotBlank(eDate)) {
           queryWrapper.apply(" date_format( sort_time, '%y-%m-%d') <=  STR_TO_DATE('"+eDate+"','%Y-%c-%d')  " );
       }
       queryWrapper.inSql(FlowerTagBook::getStorehouse,"SELECT  b.code FROM  flower_storehouse b  LEFT JOIN flower_storehouse_detail c  on b.id = c.mainid  WHERE personid = '" + sysUser.getId() + "'" );

       IPage<FlowerTagBook> pageList = flowerTagBookService.page(page, queryWrapper);
       FlowerTagBook tOtal = flowerTagBookService.getTOtal(queryWrapper);
       List<FlowerTagBook> records = pageList.getRecords();
       if(records.size()>0) {
           records.get(0).setHeji("台账数量合计："+tOtal.getNumber()+"； 锁定合计："+tOtal.getLockNumber()+"；  可用合计："+tOtal.getAbleNumber());
       }
       return Result.OK(pageList);
   }
   @ApiOperation(value="采后台账表-分页列表查询--弹窗使用", notes="采后台账表-弹窗使用")
   @GetMapping(value = "/listDialog")
   public Result<?> listDialog(FlowerTagBook flowerTagBook,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

       QueryWrapper<FlowerTagBook> queryWrapper = QueryGenerator.initQueryWrapper(flowerTagBook, req.getParameterMap());
       queryWrapper.gt("able_number",0);
       queryWrapper.inSql("storehouse","SELECT  b.code FROM  flower_storehouse b  LEFT JOIN flower_storehouse_detail c  on b.id = c.mainid  WHERE personid = '" + sysUser.getId() + "'" );
       Page<FlowerTagBook> page = new Page<FlowerTagBook>(pageNo, pageSize);
       IPage<FlowerTagBook> pageList = flowerTagBookService.page(page, queryWrapper);
       return Result.OK(pageList);
   }


   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "采后台账表-通过id查询")
   @ApiOperation(value="采后台账表-通过id查询", notes="采后台账表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       FlowerTagBook flowerTagBook = flowerTagBookService.getById(id);
       return Result.OK(flowerTagBook);
   }

 /**
  * 导出excel
  *
  * @param request
  * @param flowerTagBook
  */
 @RequestMapping(value = "/exportXls")
 public ModelAndView exportXls(HttpServletRequest request, FlowerTagBook flowerTagBook) {
     return super.exportXls(request, flowerTagBook, FlowerTagBook.class, "采后台账表");
 }

 /**
  * 通过excel导入数据
  *
  * @param request
  * @param response
  * @return
  */
 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
     return super.importExcel(request, response, FlowerTagBook.class);
 }

}
